function checkForm(form, type){
	var content = content_editor.getHtml();
	if(Prototype.Browser.IE){
		content = content.replace(/\r\n/g, "\n");
	}
	
	if(Prototype.Browser.WebKit){
		content = content.replace(/<div>/g, '\n');
	}
	
	content = content.replace(/<br[\/]?>/g, "\n").stripScripts() ;
	var reg = /<img([^>]*)>/ig;
	var finds = content.match(reg);
	try{
		var reg2 = /src=\"([^\"]*)\"/ig;
		var r = null;
		for(var i=0; i<finds.length; i++){
			if(finds[i].match(/class=[\"\']?face[\"\']?/)){
				reg2 = new RegExp('src=\"([^\"]*)\"', 'ig');
				r = reg2.exec(finds[i]);
				var img = "[img]" + r[1] + "[/img]";
				content = content.replace(finds[i], img);
			}
		}
	}catch(e){
//		alert(e.message);
	}
//	content = content.stripScripts().stripTags();
	content = content.stripScripts();
	form.content.value = content;
//	alert(form.content.value);
	var f = false;
	if(type=='send'){
		var f = checkReciever(form.reciever.value);
		if(!f){
			new MessageBox(MessageBox.TYPE_MESSAGE, '您没有填写收件人或收件人格式错误', '', {});
			return false;
		}
	}
	if(form["vcode"]){
		if(form['vcode'].value.trim()==""){
			new MessageBox(MessageBox.TYPE_MESSAGE, '您没有填写验证码', '', {});
			return false;
		}
	}
	content = form.content.value.trim();
//	alert(content);
	if(content.length<2){
		new MessageBox(MessageBox.TYPE_MESSAGE, '您输入的内容太短，最少不能低于2个字', '', {});
		return false;
	}
	return true;
}

function checkReciever(value){	
	return !(value.trim() == '');
}

function send_sms(form){
	form = $(form);
	disable_submit_btn();
	if(!checkForm(form, 'send')){
		enable_submit_btn();
		return false;
	}
	form.request({
		onComplete: function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				if(ret['result']){
					form.reset();
					content_editor.setContent('');
					location.href = "/~sms.index.view_inbox/reload/1";
					//new MessageBox(MessageBox.TYPE_MESSAGE, '短信已经发出', '成功', {});
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, '短信发送失败（' + (__lang[ret['reason']]?__lang[ret['reason']]:ret['descriptions']) + '）', '失败', {});
					enable_submit_btn();
				}
			}catch(e){}
			//vcode.reload();
		}
	});
	return false;
}

function reply_sms(form){
	form = $(form);
	disable_submit_btn();
	if(!checkForm(form, 'reply')){
		enable_submit_btn();
		return false;
	}
	form.request({
		onComplete: function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				
				if(ret['result']){
					form.reset();
					
					content_editor.setContent('');
					
					var link = (location.href.indexOf('#')>0)?location.href.substr(0, location.href.indexOf('#')):location.href;
					//var params = Object.extend(link.toQueryParams(), {reload:1});
					//location.href = $H(params).toQueryString();
					location.href = link+"?reload=1";
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE, '短信发送失败（' + __lang[ret['reason']] + '）', '失败', {});
					enable_submit_btn();
				}
			}catch(e){}
//			vcode.reload();
		}
	});
	return false;
}

function delMsgById(id){
	if(!id){
		new MessageBox(MessageBox.TYPE_MESSAGE, '参数错误', '警告', '');
		return false;
	}
	var url = "/~";
	url = url+"sms.index.refuse";
	param = "id="+id;
	var onComplete = function(transport){
		try{
			var ret = transport.responseText.evalJSON();
			if(ret.result){
				location.href = "/~sms.index.view_inbox";
			}
		}catch(e){}
	};
	new Ajax.Request(url,{
			method: 'post',
			parameters: param,
			onComplete: onComplete
		});
}

function refuseCiteNotice(){
	var url="/~sms.system.refuseCiteNotice";
	var onComplete = function(transport){
		try{
			var ret = transport.responseText.evalJSON();
			if(ret.result){
				new MessageBox(MessageBox.TYPE_MESSAGE, '设置成功', '提示', '');
				return ;
			}
		}catch(e){}
	};
	new Ajax.Request(url,{
		method: 'post',
//		parameters: param,
		onComplete: onComplete
	});
}
/*
function delete_message_handler(event){
	event = $E(event);
	var src = Event.element(event);
	var select_All = false;
	if(src.hasClassName('delete-link')){
		Event.stop(event);
		var url = src.getAttribute('href');
		var id = (url.toQueryParams()['id']);
		var onComplete = function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					if(select_All){
						location.href = "/~sms.index.view_inbox";
						//alert('testtest');
						//$("myMsgList").innerHTML = '<div class="fixpages"><div class="pages"><br/>你的'+mst_name+'是空的</div></div>' ;
						//$("myMsgList").show();
						//new Deletion('myMsgList');
					}else{
						new Deletion('row_'+id);
					}
					$("myChecked").innerHTML = 0 ;
				}
			}catch(e){}
		};
		var doAjax = function(){
			new Ajax.Request(url,{
				method: 'post',
				onComplete: onComplete
			});
		};
		var tagsname = src.innerHTML.stripTags();
		var myselect = getNumOfChecked();
		var mst_name = "收件箱";
		if(tagsname=='拒收'){
			new MessageBox(MessageBox.TYPE_OK_CANCEL, '删除此短信您将退出整个对话，您确认删除么', '', {ok: doAjax});
		}
		else if(tagsname=='清空短消息'){
			select_All = true;
			if(myselect<=0){
				new MessageBox(MessageBox.TYPE_MESSAGE, '你的收件箱目前没有任何消息！', '提示','');
				return false;
			} else
				new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除收件箱中所有的短消息，您确认删除么', '', {ok: doAjax});
		}
		else if(tagsname=='清空系统通知'){
			select_All = true;
			//var mst_name = "发件箱";
			if(myselect<=0){
				new MessageBox(MessageBox.TYPE_MESSAGE, '你的收件箱目前没有任何消息！', '提示','');
				return false;
			}else {
				new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除收件箱中所有的系统通知，您确认删除么', '', {ok: doAjax});
			}
		}
		else if(tagsname=='清空发件箱'){
			select_All = true;
			var mst_name = "发件箱";
			if(myselect<=0){
				new MessageBox(MessageBox.TYPE_MESSAGE, '你的发件箱目前没有任何消息！', '提示','');
				return false;
			} else{
				new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除发件箱中所有的短消息，您确认删除么', '', {ok: doAjax});
			}
			//new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除发件箱中所有的短消息，您确认删除么', '', {ok: doAjax});
		}
		else{
			doAjax();
		}
	}
}
*/
function delSmsSelect(app,controller,action){
	var ids = getDelSmsIds();
	var total_del = checkMySelect();
	if(!ids){
		new MessageBox(MessageBox.TYPE_MESSAGE, '您还没有选中任何要删除的短信！', '提示','');
		//ErrorTips.add("sms_del_all", 'friendly', '您还没有选中任何要删除的短信！', style);
//		showMsgBox(2,'您还没有选中任何要删除的短信！');
		return false;
	}
	else{
		//var url = "/~";
		url = "/~"+app+"."+controller+"."+action;
		param = "id="+ids;
		var onLoading = function(){
			showMsgBox(1,'短信正在删除!');
		};
		var onComplete = function(transport){
			try{
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					showMsgBox(1,'短信删除成功!');
					var tmpid = ids.split('|');
					var splitid = '';
					if(total_del[0]>=total_del[1]-2){
						//new Deletion('myMsgList');
						var vmsg = '收件箱';
						if(controller=='system' || action=='remove'){
							vmsg = "发件箱";
						}
						window.location.reload(true);
					}else{
						for(var i=0;i<tmpid.length;i++){
							if(tmpid[i].indexOf(':')>0){
								splitid = tmpid[i].split(':');
								if(splitid[1]!="undefined")
									new Deletion('row_'+splitid[1]);
							}
							else{
								new Deletion('row_'+tmpid[i]);
							}
						}
						//$("my_msg_box").hide();
						//ErrorTips.remove($("sms_del_all_1"));
						//showMsgBox(0,'');
						$$("strong").each(function(s){if(s.hasClassName("sms_nums")) {s.innerHTML = 0;}});
					}
				}
			} catch(e){}
		};
		var doRequest = function(){
			var request = new Ajax.Request(url,{
				method: 'post',
				parameters: param,
				onLoading: onLoading,
				onComplete: onComplete
			});
		};
		if(total_del[0]==total_del[1]){
			new MessageBox(MessageBox.TYPE_OK_CANCEL, '你将删除所有选定的短信，您确认删除么', '', {ok: doRequest});
		}
		else {
			doRequest();
		}
	}
}
function showMsgBox(type,msg){
	var css = 'friendly';
	switch(type){
		case 1://短信删除提示
			break;
		case 2://warning 信息
			css = "warning";
			break;
		default :
			css = "error";
			break ;
	}
	ErrorTips.add("sms_del_all_1", css, msg, '');
}

function getDelSmsIds(){
	var ids = '';
	var chkbox = document.getElementsByName('sms_id');
	var nodes = $A(chkbox);   
  	var sltNodes = nodes.select(function(node)   
   	{   
      return node.checked;   
    });   
   	sltNodes.each(function(node){  
		ids+= node.value+'|';
    });   
    return ids.substring(0,ids.length-1);
} 

function getNumOfChecked(){
	var elements = document.getElementsByTagName("input");
	var selnum=0;
	for(var i=0;i<elements.length;i++){
		if(elements[i].type=="checkbox"){
			++selnum;
		}
	}
	return selnum;
}
function checkMySelect(){
	var elements = document.getElementsByTagName("input");
	var selnum=0,total = 0;
	for(var i=0;i<elements.length;i++){
		if(elements[i].type=="checkbox"  ){
			total++;
			if(elements[i].id!="checkall" && elements[i].id!="checkall_1"){
				if(elements[i].checked){
					selnum++;
				}
			}			
		}
	}	
	if(selnum>0 && selnum<total-2){
		$("checkall").checked = false;
		$("checkall_1").checked = false;
	}
	else if(selnum==total-2){
		$("checkall").checked = true;
		$("checkall_1").checked = true;
	}
	$$("strong").each(function(s){if(s.hasClassName("sms_nums")) {s.innerHTML = selnum>=0?selnum:0;}});
	try {
		if(selnum>0)
		ErrorTips.remove($("sms_del_all_1"));
	}
	catch(e){}
	//if(selnum>0 && document.getElementById('sms_del_all_1')) ErrorTips.remove($("sms_del_all_1"));
	return new Array(selnum,total-1);
}

function checkAll(flag)
{
	var k=0;
	if(flag==1){
        $$("input").each(function(o){if(o.type=="checkbox"){if($("checkall").checked){o.checked=true;if(o.id!="checkall" && o.id!="checkall_1")k++;} else o.checked=false;$$("strong").each(function(s){if(s.hasClassName("sms_nums")) {s.innerHTML = k>=0?k:0;}});}});
     }
	else {
        $$("input").each(function(o){if(o.type=="checkbox"){if($("checkall_1").checked){o.checked=true;if(o.id!="checkall_1" && o.id!="checkall" )k++;} else o.checked=false;$$("strong").each(function(s){if(s.hasClassName("sms_nums")) {s.innerHTML = k>=0?k:0;}});}});
	}
	if(k>0) ErrorTips.remove($("sms_del_all_1"));
     // if(k>0 && $("my_msg_box").hasClassName("ufriend-tip")) $("my_msg_box").hide();
 }
 
//Event.observe(document, 'click', delete_message_handler);
//Event.observe(document,'change',checkMySelect);
//Event.observe(window, 'load', function(){ Event.observe(document.body, 'click', delete_message_handler); });
